from django.http import HttpResponse
from django.shortcuts import render
from .models import Book
from django.core.paginator import Paginator, Page
from django.views.decorators.cache import cache_page
from django.core.cache import caches


# Create your views here.


def books(request):
    bs = Book.objects.all()
    paginator = Paginator(bs, 10)
    page_num = request.GET.get("page", 1)
    page = paginator.get_page(page_num)
    return render(request, "main/book.html", context={"page": page})


def middleware(request):
    return HttpResponse("恭喜你，访问成功")


# 20s更新一次缓存，减少开销
# @cache_page(timeout=20)
def cache(request):
    # cache = caches['default']
    # print(cache.get("qiku"),"----------")
    # # 设置了一个键值对，60秒刷新一次缓存，因为使用的是本地内存，服务器重启定时会失效
    # cache.set("qiku", "hehe", 60)
    # print(cache.get("qiku"), "+++++++")

    # redis = caches['redis']
    # print(redis.get("qiku"),"----------")
    # # 设置了一个键值对，60秒刷新一次缓存，使用redis缓存，服务器重启定时也不会失效
    # redis.set("qiku", "hehe", 60)
    # print(redis.get("qiku"), "+++++++")

    book = Book.objects.first()
    # print(f"{book.title}")
    return HttpResponse("缓存页面")
